<?php
/*
  $Program: ECJC Online Shop
  $Id: subscription_customers.php
  $Based on: osCommerce, Open Source E-Commerce Solutions
  Released under the GNU General Public License
  language issues exist
*/
  defined( 'JOSCOM_VERSION' ) or die( 'Direct Access to this location is not allowed.' );
  if (!($acl->acl_check( 'administration', 'edit', 'users', $my->usertype, 'components', 'all' )
  | $acl->acl_check( 'administration', 'edit', 'users', $my->usertype, 'components', 'com_banners' ))) {
    //j15nc
  }
  if (defined( '_OSCOMMERCE_LOADED' )) {
    if (!DB_SHARED) {
      tep_db_connect() or die('Unable to connect to database server!');
    }
    global $category_links, $languages_id, $cPath, $cPath_array, $current_category_id;
    global $request_type, $lng, $cart, $currencies, $language, $navigation, $breadcrumb;
  } else {
    require(DIR_WS_COMPONENT . 'includes/application_top.php');
  }

  require(DIR_WS_CLASSES . 'currencies.php');
  $currencies = new currencies();
  
$action = (isset($_REQUEST['action']) ? $_REQUEST['action'] : '');
$cID = (isset($_REQUEST['cID']) ? $_REQUEST['cID'] : '');

?>

<link rel="stylesheet" type="text/css" href="<?php echo DIR_WS_COMPONENT ?>includes/stylesheet.css">
<script language="javascript" src="<?php echo DIR_WS_COMPONENT ?>includes/general.js"></script>

<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF" onload="SetFocus();">
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->

<!-- body //-->
<table border="0" width="100%" cellspacing="2" cellpadding="2">
  <tr>
    <td width="<?php echo BOX_WIDTH; ?>" valign="top"><table border="0" width="<?php echo BOX_WIDTH; ?>" cellspacing="1" cellpadding="1" class="columnLeft">
<!-- left_navigation //-->
<?php require(DIR_WS_INCLUDES . 'column_left.php'); ?>
<!-- left_navigation_eof //-->
    </table></td>
<!-- body_text //-->
    <td width="100%" valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="2">
<tr>
        <td><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td class="pageHeading"><?php echo HEADING_TITLE; ?></td>
            <td class="pageHeading" align="right"><?php echo tep_draw_separator('pixel_trans.gif', HEADING_IMAGE_WIDTH, HEADING_IMAGE_HEIGHT); ?></td>
          </tr>
        </table></td>
      </tr>
      <tr>
        <td><?php echo tep_draw_separator('pixel_trans.gif', '1', '8'); ?></td>
      </tr>
      <?php
      
      if($action == 'view'){
?>
<tr>
  <td>
<table border="0" width="100%" cellspacing="0" cellpadding="2">
              <tr>
                <td width="10"><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
                <td width="60"><?php echo tep_image(DIR_WS_IMAGES . 'calendar.jpg', '60', '60'); ?></td>
                <td width="10"><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
                <td><table border="0" width="100%" cellspacing="0" cellpadding="2">
<tr class="dataTableHeadingRow"><td class="dataTableHeadingContent" width=1></td><td class="dataTableHeadingContent"><?php echo TABLE_HEADING_NAME;?></td><td align=center class="dataTableHeadingContent"><?php echo TABLE_HEADING_TERMS;?></td><td align=center class="dataTableHeadingContent"><?php echo TABLE_HEADING_DATE_BILLED;?></td><td align=center class="dataTableHeadingContent"><?php echo TABLE_HEADING_DATE_PAID;?></td></tr>

<?php
$subs = tep_db_query("select * from ". TABLE_SUBSCRIPTION_CUSTOMERS ." where login='" . $cID . "'");
while($substring = tep_db_fetch_array($subs)){
$check = tep_db_query("select sq.bill_date, sq.que_id, sq.pay_date, sq.o_order_id, sq.status, pd.products_name, pd.products_description, s.price_period, s.oneday_price, s.days_as_period, s.pay_period_type from  ". TABLE_SUBSCRIPTIONS ." s, " . TABLE_SUBSCRIPTION . " sq, " . TABLE_PRODUCTS_DESCRIPTION . " pd where pd.products_id = '".$substring['productid']."' and sq.o_order_id= '".$substring['orderid']."' and s.productid = '".$substring['productid']."' and pd.language_id = '" . (int)$languages_id . "' and sq.client_id ='" . $cID . "' and sq.product_id='".$substring['productid']."' order by sq.bill_date desc ");
$checks = tep_db_fetch_array($check);
if($checks['price_period'] > '0.00'){
?>
                   <tr class="dataTableRow">
                    <td class="dataTableContent"><?php echo ' </td><td class="dataTableContent"><a href="' . tep_href_link(FILENAME_SUBSCRIPTION_CUSTOMERS, 'o_order_id=' . $checks[o_order_id] . '&action=history', 'SSL') . '">' . $checks['products_name'] . '</a></td><td class="main">' . $currencies->format($checks[price_period]) . '/';
if($checks[pay_period_type] == 'By Period'){ $terms = "Every $checks[days_as_period] days"; }
else{ $terms = $checks[pay_period_type]; } echo $terms; ?></td><td class="dataTableContent"><?php 

echo $checks[bill_date]; ?></td><td class="dataTableContent"><? echo $checks[pay_date]; ?></td>
                  </tr>
<?php
}
}
?>                  
      
                </table>

</td></tr>

<?php
      }elseif($action == 'history'){ 
?>

<table border="0" width="100%" cellspacing="1" cellpadding="2" class="infoBox" valign=top>
          <tr class="infoBoxContents">
            <td valign=top><table border="0" width="100%" cellspacing="0" cellpadding="2" valign=top>
            <tr><td class="main">Id</td><td class=main>Terms</td><td class="main">Bill Date</td><td class="main">Date Payed</td></tr>

<?

    $history_query_raw = "select sq.product_id, sq.order_id, sq.bill_date, sq.pay_date, s.price_period, s.oneday_price, s.days_as_period, s.pay_period_type  from " . TABLE_SUBSCRIPTION . " sq,". TABLE_SUBSCRIPTIONS ." s where sq.o_order_id = '" . $_GET[o_order_id] . "' and s.productid = sq.product_id and s.price_period > 0  order by sq.que_id DESC";

    $history_split = new splitPageResults($_GET['page'], MAX_DISPLAY_SEARCH_RESULTS, $history_query_raw, $history_query_numrows);
    $history_query = tep_db_query($history_query_raw);
 $n = 1;
    while ($history = tep_db_fetch_array($history_query)) {
    $i = $n +$i;
  if($history[pay_period_type] == 'By Period'){
  $terms = $currencies->display_price($history[price_period], tep_get_tax_rate('0')) . '  Every ' . $history[days_as_period] . 'days';
  } else {
  $terms = $currencies->display_price($history[price_period], tep_get_tax_rate('0')) . '  ' . $history[pay_period_type];
  }
?> <tr><td class="main"><?php
echo $i . '</td><td class=main>' . $terms . '</td><td class=main>' . $history[bill_date] . '</td><td class=main>' . $history[pay_date] . '</td></tr>';
}

?></table></td></tr><tr> <td class="main" > <table align=right><tr>
           <td class="smallText" valign="top"><?php echo $history_split->display_count($history_query_numrows, MAX_DISPLAY_SEARCH_RESULTS, $_GET['page'], TEXT_DISPLAY_NUMBER_OF_ORDERS); ?></td>
                    <td class="smallText" align="right"><?php echo $history_split->display_links($orders_query_numrows, MAX_DISPLAY_SEARCH_RESULTS, MAX_DISPLAY_PAGE_LINKS, $_GET['page'], tep_get_all_get_params(array('page', 'oID', 'action'))); ?></td>
                   </tr></table></td></tr></table>


<?php
}else{
?>
      <tr>
        <td><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td valign="top" colspan=2><table border="0" width="100%" cellspacing="0" cellpadding="2">
              <tr class="dataTableHeadingRow">
                <td class="dataTableHeadingContent"><?php echo TABLE_HEADING_NAME; ?></td>
                <td class="dataTableHeadingContent"><?php echo TABLE_HEADING_PLAN; ?></td>
                <td class="dataTableHeadingContent" align="right"><?php echo TABLE_HEADING_NBD; ?></td>
                <td class="dataTableHeadingContent" align="right"><?php echo TABLE_HEADING_ACTION; ?>&nbsp;</td>
              </tr>
<?php
$customers_query_raw = "select c.customers_id, c.customers_lastname, c.customers_firstname, c.customers_email_address, a.entry_country_id from  " . TABLE_CUSTOMERS . " c left join " . TABLE_ADDRESS_BOOK . " a on c.customers_id = a.customers_id and c.customers_default_address_id = a.address_book_id " . $search . " order by c.customers_lastname, c.customers_firstname";
$customers_query = tep_db_query($customers_query_raw);
$customers_split = new splitPageResults($_GET['page'], MAX_DISPLAY_SEARCH_RESULTS, $customers_query_raw, $customers_query_numrows);
while ($customers = tep_db_fetch_array($customers_query)) {

    $dcheck = tep_db_query("select * from " . TABLE_SUBSCRIPTION_CUSTOMERS . " where login = '" . $customers[customers_id] . "' limit 1");
      $num = tep_db_num_rows($dcheck);
      if($num > '0'){

      $info_query = tep_db_query("select customers_info_date_account_created as date_account_created, customers_info_date_account_last_modified as date_account_last_modified, customers_info_date_of_last_logon as date_last_logon, customers_info_number_of_logons as number_of_logons from " . TABLE_CUSTOMERS_INFO . " where customers_info_id = '" . $customers['customers_id'] . "' limit 1");
      $info = tep_db_fetch_array($info_query);

      $sub_info_query = tep_db_query("select sc.last_payed_date, sc.subscription_status, sc.productid, pd.products_name from " . TABLE_SUBSCRIPTION_CUSTOMERS . " sc, " . TABLE_PRODUCTS_DESCRIPTION . " pd where pd.products_id = sc.productid and pd.language_id = '" . (int)$languages_id . "' and sc.login = '" . $customers['customers_id'] . "' limit 1");
      $subinfo = tep_db_fetch_array($sub_info_query);

      if (((!$_GET['cID']) || (@$_GET['cID'] == $customers['customers_id'])) && (!$cInfo)) {


        $cInfo_array = tep_array_merge($customers, $info);
$subnew = tep_array_merge($cInfo_array, $subinfo);
        $cInfo = new objectInfo($subnew);
      }

      if ( (is_object($cInfo)) && ($customers['customers_id'] == $cInfo->customers_id) ) {
        echo '<tr class="dataTableRowSelected" onmouseover="this.style.cursor=\'hand\'" onclick="document.location.href=\'' . tep_href_link(FILENAME_SUBSCRIPTION_CUSTOMERS, tep_get_all_get_params(array('cID', 'action')) . 'cID=' . $cInfo->customers_id . '&action=view') . '\'">' . "\n";
      } else {
        echo '<tr class="dataTableRow" onmouseover="this.className=\'dataTableRowOver\';this.style.cursor=\'hand\'" onmouseout="this.className=\'dataTableRow\'" onclick="document.location.href=\'' . tep_href_link(FILENAME_SUBSCRIPTION_CUSTOMERS, tep_get_all_get_params(array('cID')) . 'cID=' . $customers['customers_id']) . '\'">' . "\n";
      }
?>
                <td class="dataTableContent"><?php echo $customers['customers_lastname'] . '  ' . $customers['customers_firstname']; ?></td>
                <td class="dataTableContent"><?php echo $cInfo->products_name; ?></td>
                <td class="dataTableContent" align="right"><?php echo tep_date_short($info['date_account_created']); ?></td>
                <td class="dataTableContent" align="right"><?php if ( (is_object($cInfo)) && ($customers['customers_id'] == $cInfo->customers_id) ) { echo tep_image(DIR_WS_IMAGES . 'icon_arrow_right.gif', ''); } else { echo '<a href="' . tep_href_link(FILENAME_CUSTOMERS, tep_get_all_get_params(array('cID')) . 'cID=' . $customers['customers_id']) . '">' . tep_image(DIR_WS_IMAGES . 'icon_info.gif', IMAGE_ICON_INFO) . '</a>'; } ?>&nbsp;</td>
              </tr>
<?php
    } }
?>
              <tr>
                <td colspan="4"><table border="0" width="100%" cellspacing="0" cellpadding="2">
                  <tr>
                    <td class="smallText" valign="top"><?php echo $customers_split->display_count($customers_query_numrows, MAX_DISPLAY_SEARCH_RESULTS, $_GET['page'], TEXT_DISPLAY_NUMBER_OF_CUSTOMERS); ?></td>
                    <td class="smallText" align="right"><?php echo $customers_split->display_links($customers_query_numrows, MAX_DISPLAY_SEARCH_RESULTS, MAX_DISPLAY_PAGE_LINKS, $_GET['page'], tep_get_all_get_params(array('page', 'info', 'x', 'y', 'cID'))); ?></td>
                  </tr>
<?php
    if (tep_not_null($_GET['search'])) {
?>
                  <tr>
                    <td align="right"><?php echo '<a href="' . tep_href_link(FILENAME_CUSTOMERS) . '">' . tep_image_button('button_reset.gif', IMAGE_RESET) . '</a>'; ?></td>
                  </tr>
<?php
    }
?>
                </table></td>
              </tr>
            </table></td>
<?php
  $heading = array();
  $contents = array();
  switch ($_GET['action']) {
    case 'confirm':
      $heading[] = array('text' => '<b>' . TEXT_INFO_HEADING_DELETE_CUSTOMER . '</b>');

      $contents = array('form' => tep_draw_form('customers', tep_href_link(FILENAME_CUSTOMERS, tep_get_all_get_params(array('cID', 'action')) . '&cID=' . $cInfo->customers_id . '&action=deleteconfirm')));
      $contents[] = array('text' => TEXT_DELETE_INTRO . '<br><br><b>' . $cInfo->customers_firstname . ' ' . $cInfo->customers_lastname . '</b>');
      if ($cInfo->number_of_reviews > 0) $contents[] = array('text' => '<br>' . tep_draw_checkbox_field('delete_reviews', 'on', true) . ' ' . sprintf(TEXT_DELETE_REVIEWS, $cInfo->number_of_reviews));
      $contents[] = array('align' => 'center', 'text' => '<br>' . tep_image_submit('button_delete.gif', IMAGE_DELETE) . ' <a href="' . tep_href_link(FILENAME_CUSTOMERS, tep_get_all_get_params(array('cID', 'action')) . 'cID=' . $cInfo->customers_id) . '">' . tep_image_button('button_cancel.gif', IMAGE_CANCEL) . '</a>');
      break;
    default:
      if (is_object($cInfo)) {
        $heading[] = array('text' => '<b>' . $cInfo->customers_firstname . ' ' . $cInfo->customers_lastname . '</b>');

        $contents[] = array('align' => 'center', 'text' => '<a href="' . tep_href_link(FILENAME_CUSTOMERS, tep_get_all_get_params(array('cID', 'action')) . 'cID=' . $cInfo->customers_id . '&action=edit') . '">' . tep_image_button('button_edit.gif', IMAGE_EDIT) . '</a> <a href="' . tep_href_link(FILENAME_CUSTOMERS, tep_get_all_get_params(array('cID', 'action')) . 'cID=' . $cInfo->customers_id . '&action=confirm') . '">' . tep_image_button('button_delete.gif', IMAGE_DELETE) . '</a> <a href="' . tep_href_link(FILENAME_ORDERS, 'cID=' . $cInfo->customers_id) . '">' . tep_image_button('button_orders.gif', IMAGE_ORDERS) . '</a> <a href="' . tep_href_link(FILENAME_MAIL, 'selected_box=tools&customer=' . $cInfo->customers_email_address) . '">' . tep_image_button('button_email.gif', IMAGE_EMAIL) . '</a>');
        $contents[] = array('text' => '<br>' . TEXT_DATE_ACCOUNT_CREATED . ' ' . tep_date_short($cInfo->date_account_created));
        $contents[] = array('text' => '<br>' . TEXT_DATE_ACCOUNT_LAST_MODIFIED . ' ' . tep_date_short($cInfo->last_payed_date));
        $contents[] = array('text' => '<br>' . TEXT_INFO_DATE_LAST_LOGON . ' '  . tep_date_short($cInfo->date_last_logon));
        $contents[] = array('text' => '<br>' . TEXT_INFO_NUMBER_OF_LOGONS . ' ' . $cInfo->number_of_logons);
      }
      break;
  }

  if ( (tep_not_null($heading)) && (tep_not_null($contents)) ) {
    echo '            <td width="25%" valign="top">' . "\n";

    $box = new box;
    echo $box->infoBox($heading, $contents);

    echo '            </td>' . "\n";
  }
?>
          </tr>
        </table></td>
      </tr>
<?php
  }



?>
    </table></td>
<!-- body_text_eof //-->
  </tr>
</table>
<!-- body_eof //-->

<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->

<?php
  require(DIR_WS_INCLUDES . 'application_bottom.php');

  // -- webMS Modification --
  // reset the Mambo Database connection (stomped by osCommerce Connection)
  if (!DB_SHARED) {
    global $database, $mosConfig_host, $mosConfig_user, $mosConfig_password, $mosConfig_db, $mosConfig_dbprefix;
    $database = new database( $mosConfig_host, $mosConfig_user, $mosConfig_password, $mosConfig_db, $mosConfig_dbprefix );
  }
?>